package com.flot.yazb.home.moudle;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flot.yazb.R;
import com.flot.yazb.base.BApplication;
import com.flot.yazb.base.BFragment;
import com.flot.yazb.SPKeyConfig;
import com.flot.yazb.base.model.BModel;
import com.flot.yazb.aop.stub.CodeStub;
import com.flot.yazb.model.user.CharacterActivity;
import com.flot.yazb.model.user.HobbyActivity;
import com.flot.yazb.model.user.UserModel;
import com.flot.yazb.home.setting.KeFuActivity;
import com.flot.yazb.home.setting.NeiXinDuBaiActivity;
import com.flot.yazb.home.setting.OrderListActivity;
import com.flot.yazb.home.setting.PayBaoYueActivity;
import com.flot.yazb.home.setting.PayDouBiActivity;
import com.flot.yazb.home.setting.PayVipActivity;
import com.flot.yazb.home.setting.UpdateMsgActivity;
import com.flot.yazb.home.setting.ZhengYouActivity;
import com.flot.yazb.widget.utils.GlideImageLoaderUtil;
import com.flot.yazb.widget.utils.SharedPreferencesUtils;
import com.flot.yazb.widget.RoundedImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 我的资料设置
 * Created by fengdongfei on 2017/8/31.
 */

public class SettingFragment extends BFragment {

    @BindView(R.id.tvxiexin)
    TextView tvXieXin;
    @BindView(R.id.icon)
    RoundedImageView icon;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.sex)
    TextView sex;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.tvIsVip)
    TextView tvIsVip;
    @BindView(R.id.tvUserType)
    TextView tvUserType;
    @BindView(R.id.tvNumCredit)
    TextView tvNumCredit;
    @BindView(R.id.ll_credit)
    LinearLayout llCredit;
    @BindView(R.id.tvVipCret)
    TextView tvVipCret;
    @BindView(R.id.ll_VipGet)
    LinearLayout llVipGet;
    @BindView(R.id.tvjilu)
    TextView tvjilu;
    @BindView(R.id.ll_jilu)
    LinearLayout llJilu;
    @BindView(R.id.ll_zhengyou)
    LinearLayout llZhengyou;
    @BindView(R.id.ll_beizhu)
    LinearLayout llBeizhu;
    @BindView(R.id.ll_tezheng)
    LinearLayout llTezheng;
    @BindView(R.id.ll_aihao)
    LinearLayout llAihao;
    @BindView(R.id.tvupdate)
    LinearLayout tvupdate;
    @BindView(R.id.tvabout)
    LinearLayout tvabout;
    @BindView(R.id.tvkefu)
    LinearLayout tvkefu;
    @BindView(R.id.id)
    TextView zid;
    Unbinder unbinder;
    private View view;
    private UserModel argsModel;
    private UserModel model = BApplication.getInstance().getUserModel();

    @Override
    protected View initRootView(LayoutInflater inflater) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.fragment_home_seet, null);
        }
        ButterKnife.bind(this, view);
        ViewGroup myparent = (ViewGroup) view.getParent();
        if (getArguments() != null) {
            argsModel = (UserModel) getArguments().getSerializable("args");
        }
        if (myparent != null) {
            myparent.removeView(view);
        }
        return view;
    }

    //利用setArgues传递参数
    public static SettingFragment newInstance(UserModel model) {
        SettingFragment fragment = new SettingFragment();
        Bundle args = new Bundle();
        args.putSerializable("args", model);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
        GlideImageLoaderUtil.loadImage(icon, model.avatar, R.mipmap.activity_splash_background);
        name.setText(model.nicheng);
        sex.setText("2".equals(model.sex) ? "女" : "男");
        address.setText(model.province + "  " + model.city);
        if (model.getIsVip()) {
            tvIsVip.setBackgroundColor(getResources().getColor(R.color.red));
        } else {
            tvIsVip.setBackgroundColor(getResources().getColor(R.color.gray_text));
        }
        tvUserType.setText("1".equals(model.type) ? "微信用户" : ("0".equals(model.type) ? "普通用户" : "官方账户"));
        tvNumCredit.setText(model.credit);
        tvVipCret.setText(model.getIsVip() == true ? "已开通" : "开通");
        if (model.getIsVip()) {
            tvVipCret.setTextColor(getResources().getColor(R.color.gray_text));
        } else {
            tvVipCret.setTextColor(getResources().getColor(R.color.red));
        }
        tvXieXin.setText(model.isBaoyue());
        zid.setText("ID: "+model.id);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @OnClick({R.id.tvVipCret, R.id.ll_jilu, R.id.ll_zhengyou, R.id.ll_beizhu, R.id.ll_tezheng, R.id.ll_aihao,
            R.id.tvupdate, R.id.tvabout, R.id.tvkefu, R.id.ll_xiexin, R.id.ll_credit, R.id.ll_VipGet, R.id.llself})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_credit:
                //豆币
                Intent intent = new Intent(getActivity(), PayDouBiActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_xiexin:
                // 写信
                Intent intent2 = new Intent(getActivity(), PayBaoYueActivity.class);
                startActivity(intent2);
                break;
            case R.id.ll_VipGet:
                // vip
                Intent intent3 = new Intent(getActivity(), PayVipActivity.class);
                startActivity(intent3);
                break;
            case R.id.ll_jilu:
                startActivity(new Intent(getActivity(), OrderListActivity.class));
                break;
            case R.id.ll_zhengyou:
                startActivity(new Intent(getActivity(), ZhengYouActivity.class));
                break;
            case R.id.ll_beizhu:
                startActivity(new Intent(getActivity(), NeiXinDuBaiActivity.class));
                break;
            case R.id.ll_tezheng:
                startActivity(new Intent(getActivity(), CharacterActivity.class));
                break;
            case R.id.ll_aihao:
                startActivity(new Intent(getActivity(), HobbyActivity.class));
                break;
            case R.id.tvupdate:
                startActivity(new Intent(getActivity(), UpdateMsgActivity.class));
                break;
            case R.id.llself:
                startActivity(new Intent(getActivity(), UpdateMsgActivity.class));
                break;
            case R.id.tvabout:

                break;
            case R.id.tvkefu:
                startActivity(new Intent(getActivity(), KeFuActivity.class));
                break;
        }
    }

    /**
     * 充值结束后重新拉去用户数据
     */
    @Override
    public void onResume() {
        super.onResume();
        Log.d("SettingFragment", "onResume: ");
        getUserMsg();
    }

    /**
     * 获取用户信息
     */
    private void getUserMsg() {
        String id = SharedPreferencesUtils.getString(getActivity(), SPKeyConfig.USERID, "");
        UserModel.getUserMsg(id, new BModel.Transit<UserModel>(getActivity()) {
            @Override
            public void onBorn(UserModel serializable, String code, String message) {
                if (code.equals(CodeStub.B_OK)) {
                    BApplication.getInstance().setUserModel(serializable);
                    SharedPreferencesUtils.saveString(getActivity(), SPKeyConfig.USERID, serializable.id);
                    model = BApplication.getInstance().getUserModel();
                    initView();
                } else {

                }
            }
        });
    }
}
